package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game19;
import com.huike.game.common.DataTools;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity19 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ImageView iv_game19_refresh;
    private TextView tv_Game19_riqi;
    private TextView tv_Game19_xinqi;
    private TextView tv_game19_wendu;
    private TextView tv_game19_shidu;
    private TextView tv_game19_pm;
    private ImageView iv_game19_animation;
    private AnimationDrawable background;
    private RequestQueue queue;
    private  ProgressDialog show;
    @SuppressLint("SimpleDateFormat")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game19);
        queue = Volley.newRequestQueue(this);
        initView();
        initData();


        iv_game19_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });



    }

    private void show (){
        show = ProgressDialog.show(this, "提示", "加载中...");
        show.show();
    }
    private void hide(){
        show.dismiss();
    }
    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "waySelect", new Response.Listener<JSONObject>() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                Game19 bean = new Gson().fromJson(jsonObject.toString(), Game19.class);

                tv_game19_pm.setText("PM2.5：" + bean.getData().getPm() + " u g/m3");
                tv_game19_shidu.setText("相对温度：" + bean.getData().getHum());
                tv_game19_wendu.setText("温度：" + bean.getData().getTem() + "°C");

                //    日期转换 //日期转换方式
                String time = bean.getData().getTime();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");
                try {
                    Date date = dateFormat.parse(time);

                    String week = DataTools.week(date.getTime() +"");

                    tv_Game19_riqi.setText(time);
                    tv_Game19_xinqi.setText(week);


                } catch (ParseException e) {
                    e.printStackTrace();
                }


//                日期转换 //时间戳转换方式
//                String time = bean.getData().getTime();
//                SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
//                String date = format.format(new Date(Long.parseLong(time)));
//                tv_Game19_riqi.setText(date);
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTime(new Date(Long.parseLong(time)));
//               int week = calendar.get(Calendar.DAY_OF_WEEK) -1;
//               if(week < 0){
//                   week =0;
//               }
//               String[] weekDay = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
//                tv_Game19_xinqi.setText(weekDay[week]);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity19.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        iv_game19_refresh = (ImageView) findViewById(R.id.iv_game19_refresh);
        tv_Game19_riqi = (TextView) findViewById(R.id.tv_Game19_riqi);
        tv_Game19_xinqi = (TextView) findViewById(R.id.tv_Game19_xinqi);
        tv_game19_wendu = (TextView) findViewById(R.id.tv_game19_wendu);
        tv_game19_shidu = (TextView) findViewById(R.id.tv_game19_shidu);
        tv_game19_pm = (TextView) findViewById(R.id.tv_game19_pm);
        iv_game19_animation = (ImageView) findViewById(R.id.iv_game19_animation);

        background  = (AnimationDrawable) iv_game19_animation.getBackground();
        background.start();


        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);

        tv_content_title.setText("路口查询");

        tv_left_text.setVisibility(View.GONE);

        iv_left_image.setImageResource(R.drawable.tiao);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        background.stop();
        background = null;
    }
}
